package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IRelationDN;
import com.fingard.dsp.bank.reqrespobj.relationdn.ReqRelationDN;
import com.fingard.dsp.bank.reqrespobj.relationdn.RetAtsRelationDN;
import com.fingard.dsp.bank.reqrespobj.relationdn.detail.RetAtsRelationDNDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CMBC01RelationDN")
public class CMBC01RelationDN extends CMBC01Base implements IRelationDN {


    @Override
    public RetAtsRelationDN d_RelationDN(ReqRelationDN p_reqDN) throws Exception {

        RetAtsRelationDN retAts = new RetAtsRelationDN(p_reqDN.reqHeader);
        retAts.writeStart();
        try {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "RelationDNForCustNo";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("xDataBody");
            xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
            // xmlWriter.writeElementString("customNo", p_reqDN.UserId);//操作员编号
            xmlWriter.writeElementString("organCode", p_reqDN.org);//组织机构代码
            xmlWriter.writeStartElement("/xDataBody");
            xmlWriter.writeStartElement("/CMBC");
            //发送报文给银行
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String Code = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String Severity = tmpBkReader.getFirstText("/CMBC/responseHeader/status/severity");
                    String Message = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(Code) && "info".equals(Severity) && "ok".equals(Message)) {
                        while (tmpBkReader.read("/CMBC/xDataBody/List/Map")) {
                            RetAtsRelationDNDetail reqRelationDNDetail = new RetAtsRelationDNDetail();
                            reqRelationDNDetail.RespCode = "0";
                            reqRelationDNDetail.RespInfo = "成功";
                            reqRelationDNDetail.OprtNo = tmpBkReader.readedXElement.getSingleText("OprtNo");
                            reqRelationDNDetail.entIdNo = tmpBkReader.readedXElement.getSingleText("entIdNo");
                            reqRelationDNDetail.cifNo = tmpBkReader.readedXElement.getSingleText("cifNo");
                            reqRelationDNDetail.cifName = tmpBkReader.readedXElement.getSingleText("cifName");
                            reqRelationDNDetail.userId = tmpBkReader.readedXElement.getSingleText("userId");
                            reqRelationDNDetail.userName = tmpBkReader.readedXElement.getSingleText("userName");
                            reqRelationDNDetail.dnVar = tmpBkReader.readedXElement.getSingleText("dnVar");
                            reqRelationDNDetail.extend = tmpBkReader.readedXElement.getSingleText("Extend");
                            retAts.writeRetDetail(reqRelationDNDetail, p_reqDN.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

}
